/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Zillow
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.media.module.activity;

import java.util.List;

import com.media.module.R;
import com.media.module.fragment.SettingFragment;
import com.media.module.fragment.TakePhotoFragment;
import com.media.module.fragment.TakeVideoFragment;
import com.media.module.model.CameraMode;
import com.media.module.utils.CameraUtils;
import com.media.module.utils.DimenValues;
import com.media.module.utils.MediaConst;
import com.media.module.utils.UIGlobalDef;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class CameraActivity extends Activity {

	private final String TAG = "CameraActivity";
	private Context mContext;
	private int mintType;
	private List<Camera.Size> msPictureSizes;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mContext = this;
		Bundle bundle = getIntent().getBundleExtra(MediaConst.BUNDLE_DATA);
		mintType = bundle.containsKey(MediaConst.CAMERA_TYPE_VALUE) ? bundle.getInt(MediaConst.CAMERA_TYPE_VALUE) : 0;
		msPictureSizes = CameraUtils.getPictureSizes(mContext);
		setContentView(getContentView());
	}

	private View getContentView() {
		FrameLayout contentView = new FrameLayout(mContext);
		contentView.setId(UIGlobalDef.VIEW_ID.FRAME_LAYOUT);
		FragmentTransaction transaction = getFragmentManager().beginTransaction();
		if (mintType == CameraMode.PHOTO.getId()) {
			transaction.replace(UIGlobalDef.VIEW_ID.FRAME_LAYOUT,
					new TakePhotoFragment(mContext, createCameraParams()));
		} else {
			transaction.replace(UIGlobalDef.VIEW_ID.FRAME_LAYOUT,
					new TakeVideoFragment(mContext, createCameraParams()));
		}
		transaction.commit();
		return contentView;
	}

	@SuppressWarnings("deprecation")
	public Bundle createCameraParams() {
		int withPx = 0;
		int heightPx = 0;
		Bundle bundle = getIntent().getBundleExtra(MediaConst.BUNDLE_DATA);
		mintType = bundle.containsKey(MediaConst.CAMERA_TYPE_VALUE) ? bundle.getInt(MediaConst.CAMERA_TYPE_VALUE) : 0;
		boolean isShowSet = bundle.containsKey(MediaConst.SHOW_PIXELS_SET) ? bundle.getBoolean(MediaConst.SHOW_PIXELS_SET) : true;
		String datas = DimenValues.getInstance(mContext).getSharePreferencesValue(UIGlobalDef.STR_RSP_TAKE_PHOTO_RESOLUTION_MAP);
		String[] resolutions = new String[0] ;
		if(!datas.isEmpty())resolutions = datas.split("-");
		if(isShowSet){
			withPx = datas.isEmpty()?msPictureSizes.get(0).width:Integer.valueOf(resolutions[0]);
			heightPx = datas.isEmpty()?msPictureSizes.get(0).height:Integer.valueOf(resolutions[1]);
		}
		if(!isShowSet){
			withPx = bundle.getInt(MediaConst.OUTPUT_PHOTO_WIDTH_PX);
			heightPx = bundle.getInt(MediaConst.OUTPUT_PHOTO_HEIGHT_PX);
		}
		bundle.putInt(MediaConst.OUTPUT_PHOTO_WIDTH_PX, withPx);
        bundle.putInt(MediaConst.OUTPUT_PHOTO_HEIGHT_PX, heightPx);
		return bundle;
		// Bundle bundle = new Bundle();
		// bundle.putInt(CameraFragment.RATIO,
		// SharedPrefManager.i.getCameraRatio());
		// bundle.putInt(CameraFragment.FLASH_MODE,
		// SharedPrefManager.i.getCameraFlashMode());
		// bundle.putInt(CameraFragment.HDR_MODE, SharedPrefManager.i.isHDR());
		// bundle.putInt(CameraFragment.QUALITY,
		// SharedPrefManager.i.getCameraQuality());
		// bundle.putInt(CameraFragment.FOCUS_MODE,
		// SharedPrefManager.i.getCameraFocusMode());
		// bundle.putBoolean(CameraFragment.FRONT_CAMERA,
		// SharedPrefManager.i.useFrontCamera());
		// return bundle;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Fragment current = getFragmentManager().findFragmentById(UIGlobalDef.VIEW_ID.FRAME_LAYOUT);
        if (current != null && current instanceof SettingFragment) {
        	Bundle bundle = ((Activity)mContext).getIntent().getBundleExtra(MediaConst.BUNDLE_DATA);
            TakePhotoFragment fragment = new TakePhotoFragment(mContext, bundle);
            getFragmentManager().beginTransaction().setCustomAnimations(R.animator.fragment_slide_left_in, R.animator.fragment_slide_right_out).replace(UIGlobalDef.VIEW_ID.FRAME_LAYOUT, fragment).addToBackStack(null).commitAllowingStateLoss();
            return;
        }
        finish();
	}

}
